package com.slx.sthq.common.utils.sql;

import cn.hutool.extra.spring.SpringUtil;
import org.apache.ibatis.logging.Log;

public class MybatisStdOutImpl implements Log {
    /**
     * 是否输出彩色sql
     */
    private static final boolean isColorSql = Boolean.parseBoolean(SpringUtil.getProperty("mybatisPlusIsPrintColorSql"));

    public MybatisStdOutImpl(String clazz) {

    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public void error(String s, Throwable throwable) {
        System.out.println(s);
        throwable.printStackTrace(System.err);
    }

    @Override
    public void error(String s) {
        System.out.println(s);
    }

    @Override
    public void debug(String s) {
        if (s.startsWith("Creating") || s.startsWith("SqlSession") || s.startsWith("JDBC") || s.startsWith("Closing") || s.contains("SqlSession")) {
            return;
        }
        //sql语句设置为蓝色、加粗、下划线
        //参考https://blog.csdn.net/soinice/article/details/97052030
        if (isColorSql && s.startsWith("==>  Preparing")) {
            s = "\033[32;1;4m" + s + "\033[0m";
        }
        if (isColorSql && s.startsWith("==> Parameters")) {
            s = "\033[31;1;4m" + s + "\033[0m";
        }
        System.out.println(s);
    }

    @Override
    public void trace(String s) {
        System.out.println(s);
    }

    @Override
    public void warn(String s) {
        System.out.println(s);
    }

}
